package com.skeleton.adminServer.guide.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.skeleton.adminServer.guide.service.IAdminGuideService;
import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.common.base.service.impl.BaseServiceImpl;
import com.skeleton.db.dao.IGuideCatgHotDao;
import com.skeleton.db.dao.IGuideItemHotDao;
import com.skeleton.db.dto.GuideCatgHotDto;
import com.skeleton.db.dto.GuideItemHotDto;
import com.skeleton.db.dto.PageDto;
import com.skeleton.db.entity.GuideCatgHot;
import com.skeleton.db.entity.GuideItemHot;

@Service
public class AdminGuideServiceImpl extends BaseServiceImpl implements IAdminGuideService {

	@Resource
	private IGuideCatgHotDao iGuideCatgHotDao;
	@Resource
	private IGuideItemHotDao iGuideItemHotDao;

	@Override
	public ResultDto doAddHotCatg(GuideCatgHotDto gchDto) {
		logger.debug("-------AdminGuideServiceImpl---doAddHotCatg-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgHot gchEntity = gchDto.toEntity();
		iGuideCatgHotDao.insert(gchEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doDeleteHotCatg(GuideCatgHotDto gchDto) {
		logger.debug("-------AdminGuideServiceImpl---doAddHotCatg-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgHot gchEntity = gchDto.toEntity();
		try {
			iGuideCatgHotDao.delete(gchEntity);
		} catch (Exception e) {
			logger.debug("数据库外键报错");
			rd.setErrorMSG("数据库外键报错");
			rd.setSuccess(false);
			return rd;
		}

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getAllHotCatg() {
		logger.debug("-------AdminGuideServiceImpl---getAllHotCatg-----------------");
		ResultDto rd = new ResultDto();

		List<GuideCatgHot> gchList = iGuideCatgHotDao.selectAll();
		for (int i = 0; i < gchList.size(); i++) {
			rd.addToDtoMap("GuideCatgHotDtoList", gchList.get(i).toDto());
		}

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getAllHotCatgByPage(PageDto pDto) {
		logger.debug("-------AdminGuideServiceImpl---getAllHotCatgByPage-----------------");
		ResultDto rd = new ResultDto();

		List<GuideCatgHot> gchList = iGuideCatgHotDao.selectAllByPage(pDto.getStart(), pDto.getRows());
		for (int i = 0; i < gchList.size(); i++) {
			rd.addToDtoMap("GuideCatgHotDtoList", gchList.get(i).toDto());
		}

		int count = iGuideCatgHotDao.countAll();
		pDto.setTotal(count);
		rd.addToDtoMap("PageDto", pDto);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doAddHotItem(GuideItemHotDto gihDto) {
		logger.debug("-------AdminGuideServiceImpl---doAddHotItem-----------------");
		ResultDto rd = new ResultDto();

		GuideItemHot gihEntity = gihDto.toEntity();
		iGuideItemHotDao.insert(gihEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getHotItemByCatgId(GuideItemHotDto gihDto) {
		logger.debug("-------AdminGuideServiceImpl---getHotItemByCatgId-----------------");
		ResultDto rd = new ResultDto();

		GuideItemHot gihEntity = gihDto.toEntity();

		List<GuideItemHot> gihList = iGuideItemHotDao.selectItemByCatgId(gihEntity);
		for (int i = 0; i < gihList.size(); i++) {
			rd.addToDtoMap("GuideItemHotDtoList", gihList.get(i).toDto());
		}

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getHotItemByCatgIdByPage(GuideItemHotDto gihDto, PageDto pDto) {
		logger.debug("-------AdminGuideServiceImpl---getHotItemByCatgIdByPage-----------------");
		ResultDto rd = new ResultDto();

		GuideItemHot gihEntity = gihDto.toEntity();

		List<GuideItemHot> gihList = iGuideItemHotDao.selectItemByCatgIdByPage(gihEntity, pDto.getStart(), pDto.getRows());
		for (int i = 0; i < gihList.size(); i++) {
			rd.addToDtoMap("GuideItemHotDtoList", gihList.get(i).toDto());
		}

		int count = iGuideItemHotDao.countItemByCatgId(gihEntity);
		pDto.setTotal(count);
		rd.addToDtoMap("PageDto", pDto);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doModifyHotCatg(GuideCatgHotDto gchDto) {
		logger.debug("-------AdminGuideServiceImpl---doModifyHotCatg-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgHot gchEntity = gchDto.toEntity();
		iGuideCatgHotDao.update(gchEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doModifyHotItem(GuideItemHotDto gihDto) {
		logger.debug("-------AdminGuideServiceImpl---doModifyHotItem-----------------");
		ResultDto rd = new ResultDto();

		GuideItemHot gihEntity = iGuideItemHotDao.selectOneById(gihDto.toEntity());
		gihEntity.setTitle(gihDto.getItemHotTitle());
		gihEntity.setUrl(gihDto.getItemHotUrl());
		gihEntity.setSeq(gihDto.getItemHotSeq());

		iGuideItemHotDao.update(gihEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doDeleteHotItem(GuideItemHotDto gihDto) {
		logger.debug("-------AdminGuideServiceImpl---doDeleteHotItem-----------------");
		ResultDto rd = new ResultDto();

		GuideItemHot gihEntity = gihDto.toEntity();
		iGuideItemHotDao.delete(gihEntity);

		rd.setSuccess(true);
		return rd;
	}

}
